﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cesave.Domain.PermisosModule.Entities;
using System.ComponentModel;
using Cesave.Domain.PermisosModule.Contracts.RiegoFuentes;
using Cesave.Infrastructure.CrossCutting.IoC;
using System.Windows.Forms;
using Cesave.Domain.Core.Entities;

namespace Cesave.DistributedServices.PermisosModule.WindowsHost.ViewModels.Catalogs.RiegoFuentes
{
	public partial class RiegoFuentesViewModel
	{
		#region Private properties

		/// <summary>
		/// 
		/// </summary>
		private List<RiegoFuente> _riegoFuentes;

		/// <summary>
		/// 
		/// </summary>
		private RiegoFuente _riegoFuente;

		#endregion

		#region Public properties

		/// <summary>
		/// 
		/// </summary>
		public List<RiegoFuente> RiegoFuentes
		{
			get
			{
				return _riegoFuentes;
			}
			private set
			{
				_riegoFuentes = value;
				if (_riegoFuentes != null)
					RaisePropertyChanged("RiegoFuentes");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public RiegoFuente RiegoFuente
		{
			get
			{
				return _riegoFuente;
			}
			private set
			{
				_riegoFuente = value;
				if (_riegoFuente != null)
					RaisePropertyChanged("RiegoFuente");
			}
		}

		#endregion

		#region Private methods

		/// <summary>
		/// 
		/// </summary>
		private void LoadRiegoFuentes()
		{
			StartWaiting();
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (sender, e) =>
				{
					IRiegoFuenteService service = IoCFactory.Resolve<IRiegoFuenteService>();
					e.Result = service.FindAllRiegoFuentes();
				};
				worker.RunWorkerCompleted += (sender, e) =>
				{
					EndWaiting();
					if (!e.Cancelled && e.Error == null)
						RiegoFuentes = e.Result as List<RiegoFuente>;
					else if (e.Error != null)
						ShowMessage(e.Error.Message);
				};
				worker.WorkerSupportsCancellation = true;
				worker.RunWorkerAsync();
			}
		}

		#endregion

		#region Event methods

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AddNewRiegoFuente(object sender, EventArgs e)
		{
			_view.riegoFuenteBindingSource.AddingNew += AddingNewRiegoFuente;
			_view.riegoFuenteBindingSource.AddNew();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AddingNewRiegoFuente(object sender, AddingNewEventArgs e)
		{
			RiegoFuente = new RiegoFuente
			{
				Descripcion = string.Empty,
				FuenteGuid = Guid.NewGuid(),
				FuenteId = 0,
				Nombre = string.Empty,
				Orden = 0,
				Status = true,
			};
			RiegoFuente.MarkAsAdded();
			e.NewObject = RiegoFuente;
			FocusDefaultControl();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SaveRiegoFuentes(object sender, EventArgs e)
		{
			StartWaiting();
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (s, args) =>
				{
					IRiegoFuenteService service = IoCFactory.Resolve<IRiegoFuenteService>();
					RiegoFuentes.ForEach(f =>
					{
						if (f.ChangeTracker.State == ObjectState.Added)
							service.AddRiegoFuente(f);
						else
							service.ChangeRiegoFuente(f);
						f.AcceptChanges();
					});
				};
				worker.RunWorkerCompleted += (s, args) =>
				{
					EndWaiting();
					if (args.Error != null)
						ShowMessage(args.Error.Message);
				};
				worker.WorkerSupportsCancellation = true;
				worker.RunWorkerAsync();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SetCurrentRiegoFuente(object sender, EventArgs e)
		{
			BindingSource bindingSource = sender as BindingSource;
			if ((bindingSource != null) && (bindingSource.Current != null))
				RiegoFuente = bindingSource.Current as RiegoFuente;
		}

		/// <summary>
		/// 
		/// </summary>
		private void OnRiegoFuentesPropertyChanged()
		{
			_view.riegoFuenteBindingSource.DataSource = RiegoFuentes;
			_view.riegoFuenteBindingSource.CurrentChanged += SetCurrentRiegoFuente;
			SetCurrentRiegoFuente(_view.riegoFuenteBindingSource, new EventArgs());
		}

		/// <summary>
		/// 
		/// </summary>
		private void OnRiegoFuentePropertyChagned()
		{
		}

		#endregion
	}
}